// pages/me/article/index.js
const app = getApp();
const common = require('../../../utils/common.js');
Page({

  /**
   * 页面的初始数据
   */
    data: {
        currentTab: 0,
        articleList_examine:[],     //审核中文章列表
        articleList_published: [],  //已发布文章列表
        page_articleList_examine:0,             //审核中文章页数标记
        page_articleList_published:0,           //已发布文章页数标记
    },
    

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this;
      wx.getSystemInfo({
          success: function(res) {
              console.log(res); //80rpx +4px
              var height=res.screenHeight+'px';
              that.setData({
                  clientHeight:height,
              })
          },
      })
          
  },
  //获取文章列表公共函数
  getArticle:function(url,article,page=0,moreFlag=false){      //page:页数
      var is_login = common.checkLogin();
      if (!is_login) return;
      var that = this;
      var token = common.getToken();
      var userId = common.getUserId();
      var start=page++*10;
      
      var data = { token: token, userId: userId ,start:start};
      
      var msg = '加载中';
      var func = function (content) {
          
          if (content.articleList && content.articleList.length>0){
              let data = {};
              if(moreFlag){
                  data[article] = that.data[article].concat(content.articleList);       //加载更多页数显示方法
              }else{
                  data[article]=content.articleList;
              }
              
              data[`page_${article}`] = page;
              that.setData(data);
          }else{
              if(page>1){   
                  common.toast('没有更多了');
              }
              
          }
          

      };
      var res = common.request(url, data, msg, func, that);   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      //获取列表
      var url_examine = `${app.apiHost}Myarticle/examine`;  //审核中文章列表接口
      var url_published = `${app.apiHost}Myarticle/published`;  //已发布文章列表接口      
      this.getArticle(url_examine, 'articleList_examine');
      this.getArticle(url_published, 'articleList_published');  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  swichNav: function (e) {
      if (this.data.currentTab === e.target.dataset.current) {
          return false;
      } else {
          this.setData({
              currentTab: e.target.dataset.current
          })
      }
  },
  bindChange: function (e) {
      this.setData({ currentTab: e.detail.current });
  },
  //审核中文章加载更多
  more_examine:function(){
      
      //获取列表
      var page=this.data.page_articleList_examine;
      var url_examine = `${app.apiHost}Myarticle/examine`;  //审核中文章列表接口
         
      this.getArticle(url_examine, 'articleList_examine',page,true);
      
  },
  //已发布文章加载更多
  more_published: function () {

      //获取列表
      var page = this.data.page_articleList_published;
      var url_examine = `${app.apiHost}Myarticle/published`;  //审核中文章列表接口

      this.getArticle(url_examine, 'articleList_published', page, true);

  },
})